package com.topera.orderservice.controller;

import com.topera.commonutils.R;
import com.topera.orderservice.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author puzhenjiang
 * @since 2020-06-12
 */
@RestController
@RequestMapping("/orderservice/paylog")
@CrossOrigin
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    /**
     * 生成二维码
     * @param orderNo
     * @return
     */
    @GetMapping("createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo){
        Map map = payLogService.createNative(orderNo);
        return R.ok().data(map);
    }

    /**
     * 查询支付状态
     * @param orderNo
     * @return
     */
    @GetMapping("getPayStatus/{orderNo}")
    public R getPayStatus(@PathVariable String orderNo){
        Map<String,String> map = payLogService.getPayStatus(orderNo);
        if(map==null){
            return R.error().message("支付失败");
        }
        if(map.get("trade_state")=="SUCCESS"){
            //向支付表中添加记录  并且更新订单表的状态
            payLogService.updateOrdersStatus(map);
            return R.ok().message("支付成功");
        }
        return R.ok().code(25000).message("支付中");
    }

}

